package com.zerui.manager.devicegroup.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zerui.manager.devicegroup.DeviceGroup;
import com.zerui.manager.devicegroup.convertor.DeviceGroupConvert;
import com.zerui.manager.devicegroup.entity.DeviceGroupDo;
import com.zerui.manager.devicegroup.gateway.DeviceGroupGateWay;
import com.zerui.manager.devicegroup.mapper.DeviceGroupMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Component
public class DeviceGroupGateWayImpl implements DeviceGroupGateWay {

    @Resource
    private DeviceGroupMapper deviceGroupMapper;

    @Override
    public DeviceGroup selectById(Long supId) {
        DeviceGroupDo deviceGroupDo = deviceGroupMapper.selectById(supId);
        if(ObjectUtils.isEmpty(deviceGroupDo)){
            return null;
        }
        return DeviceGroupConvert.INSTANCE.convertDeviceGroupDoToDeviceGroup(deviceGroupDo);
    }

    @Override
    public DeviceGroup getNodeByName(String orgName) {
        LambdaQueryWrapper<DeviceGroupDo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DeviceGroupDo::getOrgName,orgName);
        DeviceGroupDo deviceGroupDo = deviceGroupMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(deviceGroupDo)){
            return null;
        }
        return DeviceGroupConvert.INSTANCE.convertDeviceGroupDoToDeviceGroup(deviceGroupDo);
    }

    @Override
    public void addDeviceGroup(DeviceGroup deviceGroup) {
        DeviceGroupDo deviceGroupDo = DeviceGroupConvert.INSTANCE.convertDeviceGroupToDeviceGroupDo(deviceGroup);
        deviceGroupMapper.insert(deviceGroupDo);
    }

    @Override
    public void updateById(DeviceGroup deviceGroup) {
        DeviceGroupDo deviceGroupDo = DeviceGroupConvert.INSTANCE.convertDeviceGroupToDeviceGroupDo(deviceGroup);
        deviceGroupMapper.updateById(deviceGroupDo);
    }

    @Override
    public void deleteById(Long id) {
        deviceGroupMapper.deleteById(id);
    }

    @Override
    public List<DeviceGroup> selectBatchIds(Set<Long> orgIds) {
        List<DeviceGroupDo> deviceGroupDos = deviceGroupMapper.selectBatchIds(orgIds);
        return DeviceGroupConvert.INSTANCE.convertDeviceGroupDosToDeviceGroups(deviceGroupDos);
    }

    @Override
    public void updateBatch(List<DeviceGroup> deviceGroups) {
        List<DeviceGroupDo> deviceGroupDos = DeviceGroupConvert.INSTANCE.convertDeviceGroupsToDeviceGroupDos(deviceGroups);
        deviceGroupMapper.updateBatch(deviceGroupDos);
    }

    @Override
    public List<DeviceGroup> getDeviceGroupBySupId(Long supId) {
        LambdaQueryWrapper<DeviceGroupDo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DeviceGroupDo::getSupId,supId);
        List<DeviceGroupDo> deviceGroups = deviceGroupMapper.selectList(queryWrapper);
        return DeviceGroupConvert.INSTANCE.convertDeviceGroupDosToDeviceGroups(deviceGroups);
    }

    @Override
    public Map<Long, DeviceGroup> getDeviceGroupMapByIds(Set<Long> deviceList) {

        LambdaQueryWrapper<DeviceGroupDo> queryWrapper = Wrappers.lambdaQuery();
        List<DeviceGroupDo> deviceGroupDos = deviceGroupMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(deviceGroupDos)){
            List<DeviceGroup> deviceGroups = DeviceGroupConvert.INSTANCE.convertDeviceGroupDosToDeviceGroups(deviceGroupDos);
            Map<Long, DeviceGroup> deviceGroupMap = new HashMap<>(16);
            DeviceGroup root = null;
            for (Long id:deviceList){
                for (DeviceGroup deviceGroup:deviceGroups){
                    if(ObjectUtils.isEmpty(root) && ObjectUtils.isEmpty(deviceGroup.getSupId())){
                        root = deviceGroup;
                    }
                    Set<String> deviceIds = deviceGroup.getDeviceIds();
                    if(CollectionUtils.isNotEmpty(deviceIds) && deviceIds.contains(id.toString())){
                        deviceGroupMap.put(id,deviceGroup);
                        break;
                    }
                    deviceGroupMap.put(id,root);
                }

            }
            return deviceGroupMap;
        }
        return Collections.emptyMap();
    }

}
